<?php
/**
 * 《领导关系》
 *
 * @author 孙晓晔
 * @version $Id: Leader.class.php 1037 2010-03-26 11:47:59Z sunxy $
 */

require_once(dirname(__FILE__) . '/EmpUtil.class.php');

class Leader extends EmpUtil {
	public $tbl = Constant::tbl_emp_leader;

	public $orderby = array(
		'-' => '请选择',
		'dept' => '部门',
		'leader' => '领导者',
		'emp' => '被领导',
		'1' => '创建时间',
		'2' => '更新时间',
	);
		
	function __construct() {
		$request['update'] = '$Date: 2009/03/06 11:35:48 $';
		$request['revision'] = '$Revision: 1.2 $';
			
		parent::__construct($request);
	}

	/**
	 * 预处理
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function pretreat($controller) {
		parent::pretreat($controller);
	}

	/**
	 * 首页
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function layout($controller, $request) {

		$controller->display($request, 'priv');
		return true;
	}

	/**
	 * 查找
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
			
		$controller->display($request, 'emp/leader/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}
	
	function _where($request) {
		$leader_s = $this->_input($request['leader_s']);
		
		$where = " where 1 = 1";

		if (!empty($leader_s)) {
			$where .= " AND leader = '" . $leader_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);
		
		return $where;
	}

	/**
	 * 列表
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->findPara($controller, $request);		
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {
			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
				
				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'emp/leader/list');
		return true;
	}

	/**
	 * 添加
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);
		
		$db = newdb($this->dsn_r);
		$data = array();
		$fmt = "SELECT * FROM %s WHERE status = '1'";
		$sql = sprintf($fmt, Constant::tbl_emp);
		// debug($sql);
		$result = $db->query($sql);
		$controller->assign_by_ref('result', $db->fetch_all($result));
		
		$controller->display($request, 'emp/leader/add');
		return true;
	}

	/**
	 * 添加
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$leader	= $this->_input($request['leader']);
		$list = $request['list'];

		$bb = explode(',', $list);
			
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(dept, leader, emp, createtime) VALUES('%s', '%s', '%s', now())";
		while (list($k, $emp) = each($bb)) {
			if ($leader == $emp) {
				continue;
			}
			
			$row = $this->id(Constant::tbl_emp, $leader);
			
			$sql = sprintf($fmt, $this->tbl, $row['dept'], $leader, $emp);
			// debug($sql);
			$db->query($sql);
		}
		
		clean("leader_" . $leader);
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'emp/leader/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$leader = $this->_input($request['leader']);
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET leader = '%s', emp = '%s', status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $leader, $emp, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'emp/leader/copy');
		return true;
	}
	
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'emp/leader/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['leader_x'] = $this->_emp_x($row['leader']);
		if ($row['leader_x']['status'] == '1') {
			$row['leader_t'] = $row['leader_x']['name'];
		} else {
			$row['leader_t'] = sprintf("%s%s", $row['leader_x']['name'], $row['leader_x']['status']);
		}
	
		$row = $this->_view_t($row);
	
		return $row;
	}

	/**
	 * 删除
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {			
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$insql = " in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		$sql = "DELETE FROM " . $this->tbl . " WHERE id " . $insql;
		$query = $db->query($sql);
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _list($emp) {
		
		$db = newdb($this->dsn_r);
		// $fmt = "SELECT * FROM %s WHERE dept = 'bj' AND emp = '%s'";
		$fmt = "SELECT * FROM %s WHERE emp = '%s'";
		$sql = sprintf($fmt, $this->tbl, $emp);
		debug($sql);
		$result = $db->query($sql);
		
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$ret[] = $row['leader'];
		}

		return $ret;
	}
}
?>
